package com.hgq.jssjyyxt.util;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

@Slf4j
public class DateUtil {
    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    /**
     * 获取当天时间零时零分零秒
     *
     * @return
     */
    public static String beginTime() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " 00:00:00";
    }

    /**
     * 获取当天时间零时零分零秒
     *
     * @return
     */
    public static String endTime() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + " 59:59:59";
    }

    //转换时间参数是一个时间字符串，转换为LocalDateTime类型
    public static LocalDateTime convertTime(String time) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = df.parse(time);
        return LocalDateTime.ofInstant(date.toInstant(), TimeZone.getDefault().toZoneId());
    }

    /**
     * 获取两个时间相差分钟数
     *
     * @param oldTime
     * @param newTime
     * @return
     * @throws ParseException
     */
    public static long chaTime(String oldTime, String newTime) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long NTime = df.parse(newTime).getTime();
        long OTime = df.parse(oldTime).getTime();
        long diff = (NTime - OTime) / 1000 / 60;
        return diff;
    }

    //格式化时间
    public static Date formatTime(String time) throws ParseException {
        // 解析时间戳
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        return sdf.parse(time);
//        log.info("前端获取的时间：date: "+date);
//        // 获取年月日时分秒
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date);
//        int year = calendar.get(Calendar.YEAR);
//        int month = calendar.get(Calendar.MONTH) + 1;
//        int day = calendar.get(Calendar.DAY_OF_MONTH);
//        int hours = calendar.get(Calendar.HOUR_OF_DAY);
//        int minutes = calendar.get(Calendar.MINUTE);
//        int seconds = calendar.get(Calendar.SECOND);
//
//        log.info("前端获取的时间：Year: " + year + ", Month: " + month + ", Day: " + day + ", Hours: " + hours + ", Minutes: " + minutes + ", Seconds: " + seconds);
//        String newDates= String.format("%d-%d-%d %d:%d:%d", year, month, day, hours, minutes, seconds);
//        log.info("前端获取的时间：newDate: "+newDates);
//        log.info("前端获取的时间：newDate: "+LocalDateTime.parse(newDates));
//        // 转换为中国标准时间（CST，UTC+8）
//        Calendar cstCalendar = Calendar.getInstance();
//        cstCalendar.setTime(date);
//        cstCalendar.add(Calendar.HOUR_OF_DAY, 8);
//
//        // 重新获取转换后的年月日时分秒
//        int cstYear = cstCalendar.get(Calendar.YEAR);
//        int cstMonth = cstCalendar.get(Calendar.MONTH) + 1;
//        int cstDay = cstCalendar.get(Calendar.DAY_OF_MONTH);
//        int cstHours = cstCalendar.get(Calendar.HOUR_OF_DAY);
//        int cstMinutes = cstCalendar.get(Calendar.MINUTE);
//        int cstSeconds = cstCalendar.get(Calendar.SECOND);
//
//        // 打印CST时间
//        log.info("转换后的时间是： Year: " + cstYear + ", CST Month: " + cstMonth + ", CST Day: " + cstDay + ", CST Hours: " + cstHours + ", CST Minutes: " + cstMinutes + ", CST Seconds: " + cstSeconds);
//        String newDate= String.format("%d-%d-%d %d:%d:%d", cstYear, cstMonth, cstDay, cstHours, cstMinutes, cstSeconds);
//        return LocalDateTime.parse(newDate);
    }

}
